package cn.lsh.main;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public abstract class BaseDriver {
	protected static Job job;
	protected static Configuration conf;

	protected static void buildJob(boolean loadDefault) throws Exception {
		conf = new Configuration(loadDefault);
		job = Job.getInstance(conf);
		job.setJarByClass(BaseDriver.class);
	}

	protected static void setInputAndOutPut(String[] args, boolean isDelete) throws Exception {
		if (args == null || args.length == 0) {
			return;
		}
		FileInputFormat.addInputPath(job, new Path(args[0]));
		Path output = new Path(args[1]);
		if (isDelete) {
			FileSystem fs = output.getFileSystem(conf);
			if (fs.exists(output)) {
				fs.delete(output, true);
			}
		}
		FileOutputFormat.setOutputPath(job, output);
	}
}
